#include "LayoutEngine.h"
#include "Panel.h"

using namespace GUIToolkit;

void VerticalLayout::Layout(Panel* panel)
{
	int count = panel->Count();
	SizeF size = panel->GetSize();
	float y = 0;
	for(int i = 0; i< count; ++i)
	{
		UIElement* e = (*panel)[i];
		if(e->IsLayoutEnable())
		{
			LayoutInfo info;
			e->GetLayoutInfo(&info);
			SizeF es = e->GetSize();
			y += info.Margin.Top;
			float x = info.Margin.Left;
			if(info.Horizontal == Align::AlignCenter)
				x = (size.Width - info.Margin.Left - info.Margin.Right - es.Width) / 2.0f;
			else if(info.Horizontal == Align::AlignRightButtom)
				x = size.Width - info.Margin.Right - es.Width;

			e->SetRegion(x, y,
				info.FixedWidth ? es.Width : (size.Width - info.Margin.Left - info.Margin.Right),
				es.Height);
			y += es.Height;
			y += info.Margin.Buttom;
		}
	}
}

void HorizontalLayout::Layout(Panel* panel)
{
	int count = panel->Count();
	SizeF size = panel->GetSize();
	float x = 0;
	for(int i = 0; i< count; ++i)
	{
		UIElement* e = (*panel)[i];
		if(e->IsLayoutEnable())
		{
			LayoutInfo info;
			e->GetLayoutInfo(&info);
			SizeF es = e->GetSize();
			x += info.Margin.Top;

			float y = info.Margin.Top;
			if(info.Vertical == Align::AlignCenter)
				y = (size.Height - info.Margin.Top - info.Margin.Buttom - es.Height) / 2.0f;
			else if(info.Vertical == Align::AlignRightButtom)
				y = size.Height - info.Margin.Buttom - es.Height;

			e->SetRegion(x, y, es.Width,
				info.FixedHeight ? es.Height : (size.Height - info.Margin.Top - info.Margin.Buttom));
			x += es.Width;
			x += info.Margin.Right;
		}
	}
}